package com.hzhr.iot.user.controller;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.Author;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.user.controller.base.BaseController;
import com.hzhr.iot.user.model.dept.DeptDeleteModel;
import com.hzhr.iot.user.model.dept.DeptGetModel;
import com.hzhr.iot.user.model.dept.DeptPostModel;
import com.hzhr.iot.user.service.DeptService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

@Api(tags = "Dept", description = "部门")
@RestController
@RequestMapping(value = "")
public class DeptController extends BaseController {
    @Autowired
    DeptService deptService;

    //region /depts POST
    //region swagger响应码 : post
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 403, message = "----异常代码 : 201  \n----异常类型 : 业务流程异常  \n----异常业务 : 验证上级部门  \n----异常说明 : 上级部门不存在  \n\n----异常代码 : 202  \n----异常类型 : 业务流程异常  \n----异常业务 : 验证部门名称  \n----异常说明 : 部门名称已存在  \n\n", response = ResponseExceptionDto.class),
    })
    //endregion
    //region swagger说明 : post
    @ApiOperation(value="在登录用户的公司下创建非顶级部门", notes="在登录用户的公司下创建非顶级部门  \n简介 : 在登录用户的公司下创建非顶级部门  \n术语 :  \n1. 部门(DEPT)的直接下级部门：  \n 满足以下条件的部门(DEPT)对象dept_1可以称作部门(DEPT)对象dept_2的直接下级部门：  \n (1) dept_1.parentId = dept_2；  \n步骤 :  \n1. 验证上级部门编号 :  \n 查询部门(DEPT)对象dept_1，查询规则如下：  \n (1) dept_1.id = body.parentId；  \n (2) dept_1.companyId = 登录用户.companyId；  \n 如果dept_1不存在，抛出异常201；  \n2. 验证部门名称 :  \n 如果存在部门(DEPT)对象dept满足以下条件，则抛出异常202：  \n (1) dept.name = body.name  \n3. 创建部门 :  \n 根据body创建部门(DEPT)对象dept_2，并设置以下属性：  \n (1) dept_2.code的设置规则：  \n 如果dept_1不存在直接下级部门，那么：dept_2.code = dept_1.code + '01'；  \n 如果dept_1存在直接下级部门，查询出dept_1的直接下级部门中code值最大的部门(DEPT)对象，提取出该对象的code的最后两位，转换为数字并在此基础上 +1 ，  \n 然后左补零至两位字符串str_1，最后：dept_2.code = dept_1.code + str_1；  \n (2) dept_2.resourceCode的设置规则：  \n 如果dept_1不存在直接下级部门，那么：dept_2.resourceCode = dept_1.resourceCode + '01'；  \n 如果dept_1存在直接下级部门，查询出dept_1的直接下级部门中resourceCode值最大的部门(DEPT)对象，提取出该对象的resourceCode的最后两位，转换为数字并在此基础上 +1 ，  \n 然后左补零至两位字符串str_1，最后：dept_2.resourceCode = dept_1.resourceCode + str_1；  \n (3) dept_2.level = dept_1.level + 1；  \n (4) dept_2.parentName = dept_1.name；  \n (5) dept_2.companyId = dept_1.companyId；  \n4. 返回信息 :  \n 调用“查询部门”服务(user-service: /depts GET)获取“部门模型(查询)”对象result，调用规则如下：  \n (1) accessMode: GLOBAL  \n (2) query: _equal_id = dept_2.id  \n 设置result属性，设置规则如下：  \n (1) result.changeCount = 创建数量；  \n 返回result；  \n",produces = "application/json")
    //endregion
    //region swagger参数 : post
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    //endregion
    //region 请求 : post
    @RequestMapping(value = "/depts", method = RequestMethod.POST)
    //endregion
    @Author
    public ResponseEntity<DeptGetModel> post(@RequestParam @ApiIgnore Map<String, Object> query,
                                                       @RequestBody DeptPostModel body) throws ServiceException {
        DeptGetModel responseBody = deptService.post(initHeader(), query, body);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
        //endregion
    }
    //endregion
    //region /depts/{deptId} DELETE
    //region swagger响应码 : deleteById
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 403, message = "----异常代码 : 201  \n----异常类型 : 业务流程异常  \n----异常业务 : 查询部门  \n----异常说明 : 部门不存在  \n\n----异常代码 : 202  \n----异常类型 : 业务流程异常  \n----异常业务 : 验证部门是否具备删除条件  \n----异常说明 : 存在下级部门或下级用户，无法删除  \n\n", response = ResponseExceptionDto.class),
    })
    //endregion
    //region swagger说明 : deleteById
    @ApiOperation(value="在登录用户的公司下删除非顶级部门", notes="在登录用户的公司下删除非顶级部门  \n简介 : 删除部门  \n术语 :  \n1. 部门(DEPT)的直接下级部门：  \n 满足以下条件的部门(DEPT)对象dept_1可以称作部门(DEPT)对象dept_2的直接下级部门：  \n (1) dept_1.parentId = dept_2；  \n2. 部门(DEPT)的下级用户：  \n 满足以下条件的用户(USER)对象user_1可以称作部门(DEPT)对象dept_1的下级用户：  \n (1) user_1.dept_id = dept_1.id；  \n步骤 :  \n1. 查询部门 :  \n 根据路径参数deptId查询部门(DEPT)dept_1，查询规则如下：  \n (1) dept_1.id = 路径参数deptId；  \n 如果dept_1不存在，抛出异常201；  \n2. 验证部门是否具备删除条件 :  \n 如果dept_1存在直接下级部门或下级用户，则抛出异常202；  \n3. 删除部门 :  \n 删除dept_1；  \n4. 返回信息 :  \n 生成“部门模型(删除)”对象result，并设置以下属性：  \n (1) result.changeCount = 删除数量；  \n 返回result；  \n",produces = "application/json")
    //endregion
    //region swagger参数 : deleteById
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "deptId", value = "部门编号",required = true, dataType = "int", paramType = "path"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : deleteById
    @RequestMapping(value = "/depts/{deptId}", method = RequestMethod.DELETE)
    //endregion
    @Author
    public ResponseEntity<DeptDeleteModel> deleteById(@PathVariable("deptId") Integer deptId,
                                                      @RequestParam @ApiIgnore Map<String, Object> query) throws ServiceException {
        DeptDeleteModel responseBody = deptService.deleteById(initHeader(), deptId, query);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
        //endregion
    }
    //endregion
    //region /depts GET
    //region swagger响应码 : get
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
    })
    //endregion
    //region swagger说明 : get
    @ApiOperation(value="查询部门", notes="查询部门  \n简介 : 查询部门  \n步骤 :  \n1. 查询部门 : (√)  \n 根据条件参数查询部门(DEPT)；  \n2. 返回信息 : (√)  \n 根据条件参数和查询到的结果组装响应并返回，响应模型的部分属性获取规则如下：  \n (1) “部门模型”对象deptModel_1.managementName的获取规则：  \n 如果查询结果部门(DEPT)对象dept_1.level 小于等于 3，deptModel_1.managementName = dept_1.name，  \n 否则，取出dept_1.code的前9位字符串str_1，并查询出部门(DEPT)dept_2，其中dept_2.code = str_1，  \n 最后，deptModel_1.managementName = dept_2.name；  \n (2) “部门模型”对象deptModel_1.subCompanyName的获取规则：  \n 如果查询结果部门(DEPT)对象dept_1.level 小于等于 2，deptModel_1.subCompanyName = dept_1.name，  \n 否则，取出dept_1.code的前7位字符串str_1，并查询出部门(DEPT)dept_2，其中dept_2.code = str_1，  \n 最后，deptModel_1.subCompanyName = dept_2.name；  \n",produces = "application/json")
    //endregion
    //region swagger参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_managementName", value = "是否展示管理站名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_subCompanyName", value = "是否展示分公司名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "is_paging", value = "页数", defaultValue = "true",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where DeptModel.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "in_id", value = "筛选条件 : where DeptModel.id in #{in_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_name", value = "筛选条件 : where DeptModel.name = #{equal_name}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_level", value = "筛选条件 : where DeptModel.level = #{equal_level}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_parentId", value = "筛选条件 : where DeptModel.parentId = #{equal_parentId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_companyId", value = "筛选条件 : where DeptModel.companyId = #{equal_companyId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_code", value = "筛选条件 : where DeptModel.code = #{equal_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "lessthanorequal_level", value = "筛选条件 : where DeptModel.level <= #{lessthanorequal_level}",required = false, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_managementName", value = "是否展示管理站名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_subCompanyName", value = "是否展示分公司名称", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_is_paging", value = "页数", defaultValue = "true",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where DEPT.id = #{_equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_in_id", value = "筛选条件 : where DEPT.id in #{_in_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_name", value = "筛选条件 : where DEPT.name = #{_equal_name}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_companyId", value = "筛选条件 : where DEPT.companyId = #{_equal_companyId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_level", value = "筛选条件 : where DEPT.level = #{_equal_level}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_parentId", value = "筛选条件 : where DEPT.parentId = #{_equal_parentId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_like_resourceCode", value = "筛选条件 : where DEPT.resourceCode like #{_like_resourceCode}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_like_code", value = "筛选条件 : where DEPT.code like #{_like_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_code", value = "筛选条件 : where DEPT.code = #{_equal_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_lessthanorequal_level", value = "筛选条件 : where DEPT.level <= #{_lessthanorequal_level}",required = false, dataType = "int", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/depts", method = RequestMethod.GET)
    //endregion
    @Author
    public ResponseEntity<DeptGetModel> get(@RequestParam @ApiIgnore Map<String, Object> query) throws ServiceException {
        DeptGetModel responseBody = deptService.get(initHeader(), query);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
        //endregion
    }
    //endregion
}
















































































































































































































































































































































































